<?php

namespace App\Http\Controllers\Mobile;


use App\CrmCase;
use App\CrmProduct;
use App\CrmProductCategory;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class CaseController extends Controller
{
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $service_id = Input::get('service_id', 0);
//        dump($service_id);
        #如果case_id不为0 获取该id 分类信息
        $id = array();
        if ($service_id != 0) {
//            $three = CrmCase::where('id',$service_id)->first();dump($three);
//            $id['three_id'] = $three->product_id;
            $two = CrmProduct::where('id', $service_id)->first();
            $id['two_id'] = $two->category;
            $one = CrmProductCategory::where('id', $id['two_id'])->first();
            $id['one_id'] = $one->parent_id;
        } else {
            $id = [
                'two_id' => 0,
                'one_id' => 0
            ];
        }

        #获取分类信息
        $info = $this->productCate();
        return view('mobile.case', compact('info', 'service_id', 'id'));
    }

    /**
     * @param Request $request
     * @return array
     */
    public function getInfo(Request $request)
    {
        $take = $request->input('e',6);
        $page = $request->input('page',1);
        $skip = ($page-1)*$take;
        $obj = new CrmCase();
        $service = $request->input('service');
        if($service) {
            $obj = $obj->where('product_id',$service);
        }
        $num = $obj->where('status',1)->count();
        $info = $obj->where('status',1)->orderBy('rank','desc')->orderBy('create_time','desc')->skip($skip)->take($take)->get();
        if($info){
            return [
                'code'=>100,
                'num'=>$num,
                'page'=>$page,
                'data'=>$info,
                'service'=>$service
            ];
        }else{
            return [
                'code'=>101,
                'msg'=>'获取失败'
            ];
        }

    }

    /**
     * @param $id 案例id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function detail($id)
    {
        #根据id获取详情
        $detail = CrmCase::where('id',$id)->first();
        $detail->edit = str_replace('<img src="/','<img src="http://manage.vipyipoint.com/',$detail->edit);
        $detail->edit = str_replace('<img src="http://www.vipyidian.com/','<img src="http://www.vipyipoint.com/',$detail->edit);
        $detail->edit = str_replace('<img src="http://vipyidian.com/','<img src="http://www.vipyipoint.com/',$detail->edit);
        $detail->edit = str_replace('width:636px','width:100%',$detail->edit);
        $detail->edit = str_replace('text-indent:24px','text-indent:0px',$detail->edit);
        $detail->edit = str_replace('text-indent:2em','text-indent:',$detail->edit);
        return view('mobile.detail',compact('detail'));
    }

    public function jump($id)
    {

    }
}
